package com.geezdata.cps.ttk.query.query.impl;

import java.util.List;
import java.util.Map;

import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.geezdata.cps.base.utils.BeanUtil;
import com.geezdata.cps.ttk.common.vo.RoleResVO;
import com.geezdata.cps.ttk.common.vo.UserRoleResVO;
import com.geezdata.cps.ttk.domain.domain.UserRole;
import com.geezdata.cps.ttk.mapper.mapper.UserRoleMapper;
import com.geezdata.cps.ttk.query.query.RoleService;
import com.geezdata.cps.ttk.query.query.UserRoleService;
import com.google.common.collect.Maps;


@Service
public class UserRoleServiceImpl implements UserRoleService {
	@Autowired
    private UserRoleMapper userRoleMapper;
	
	@Autowired
	private RoleService roleService;
	
	@Override
	public List<UserRoleResVO> getUserRoleList( Long userId, Long roleId, Integer status) {
		Map<String, Object> params = Maps.newHashMap();
		params.put("status", status);
		params.put("userId", userId);
		params.put("roleId", roleId);
		
		List<UserRole> userRoleList = userRoleMapper.selectBatch(params);
		if (CollectionUtils.isEmpty(userRoleList)) {
			return null;
		}
		
		List<UserRoleResVO> userRoleVOList = BeanUtil.convertList(userRoleList, UserRoleResVO.class);
		for (UserRoleResVO userRoleVO : userRoleVOList) {
			setRoleExt(userRoleVO);
        }
		
		return userRoleVOList;
	} 
	
	private void setRoleExt(UserRoleResVO userRoleVO) {
		RoleResVO roleVO = roleService.getRoleDetail(userRoleVO.getRoleId());
		userRoleVO.setRole(roleVO);
	}
}